import { useEffect, useState } from 'react';
import { VARIABLE_TYPE } from 'src/utils/constant';
import useDataSourceData from './useDataSourceData';

/** @method useLimitData
 *  @description 取可选的数据
 *  @param {Object} data
 *  @param {Array} limitType
 *  @param {Array} dataType
 *  @param {Object} strategyId 可选
 *       @param {String} strategyId
 *       @param {String} fieldId
 *  @return Array
 */

export default function useLimitData(data, limitType, dataType, { strategyId, fieldId }, leftVar) {
  const dataSourceType = useDataSourceData().dataSourceType;

  function getKeyName(key) {
    const data = {
      [VARIABLE_TYPE._FIELD]: {
        name: VARIABLE_TYPE._FIELD,
        key: VARIABLE_TYPE._FIELD,
        label: '变量',
      },
      [VARIABLE_TYPE._FEATURE]: {
        name: VARIABLE_TYPE._FEATURE,
        key: VARIABLE_TYPE._FEATURE,
        label: '指标',
      },
      [VARIABLE_TYPE._MODEL]: {
        name: VARIABLE_TYPE._MODEL,
        key: VARIABLE_TYPE._MODEL,
        label: '模型',
      },
      [VARIABLE_TYPE._NAMELIST]: {
        name: VARIABLE_TYPE._NAMELIST,
        key: VARIABLE_TYPE._NAMELIST,
        label: '名单',
      },
      [VARIABLE_TYPE._THIRD_FEATURE]: {
        name: VARIABLE_TYPE._THIRD_FEATURE,
        key: VARIABLE_TYPE._THIRD_FEATURE,
        label: '外部数据',
      },
      [VARIABLE_TYPE._STRATEGY]: {
        name: VARIABLE_TYPE._STRATEGY,
        key: VARIABLE_TYPE._STRATEGY,
        label: '组件',
      },
      [VARIABLE_TYPE._LITERAL]: {
        name: VARIABLE_TYPE._LITERAL,
        key: VARIABLE_TYPE._LITERAL,
        label: '常量',
      },
      [VARIABLE_TYPE._RISK_LEVEL]: {
        name: VARIABLE_TYPE._RISK_LEVEL,
        key: VARIABLE_TYPE._RISK_LEVEL,
        label: '风险等级',
      },
      [VARIABLE_TYPE._ENUM]: {
        name: VARIABLE_TYPE._ENUM,
        key: VARIABLE_TYPE._ENUM,
        label: '枚举值',
      },
      ...dataSourceType,
    };
    return data[key];
  }

  function getAllData() {
    const variableDataKeys = Object.keys(data).filter((key) => key !== VARIABLE_TYPE._FEATURE_ALL);

    const allData = variableDataKeys
      .filter((key) => {
        return limitType ? limitType.includes(key) : true;
      })
      .map((key) => {
        if (data[key] instanceof Array) {
          const newData = data[key].map((item) => {
            if (
              (dataType && item.data_type && !dataType.includes(item.data_type)) ||
              (strategyId && key === VARIABLE_TYPE._STRATEGY && Number(item.code) === Number(strategyId)) ||
              (fieldId && key === VARIABLE_TYPE._FIELD && Number(item.field_id) === Number(fieldId))
            ) {
              item.disabled = true;
            } else {
              delete item.disabled;
            }
            // 如果是strategy，则默认加上风险等级作为二级列表
            // if (key === VARIABLE_TYPE._STRATEGY) {
            //   item.children = [
            //     {
            //       name: '风险等级',
            //       code: 'RiskLevel',
            //     },
            //   ];
            // }
            return Object.assign({}, item);
          });
          return {
            ...getKeyName(key),
            content: newData,
          };
        } else {
          return {
            ...getKeyName(key),
            content: data[key],
          };
        }
      });
    if (leftVar && leftVar.type === VARIABLE_TYPE._FIELD) {
      const fieldObj = allData.find((item) => item.key === VARIABLE_TYPE._FIELD) || {};
      const targetCode = leftVar.code.split('.');
      let enumeration = [];
      fieldObj.content && fieldObj.content.forEach((item) => {
        if (item.code === targetCode[0]) {
          item.children.forEach((item) => {
            if (item.code === targetCode[1]) {
              enumeration = item.varEnumVos;
            }
          });
        }
      });
      if (enumeration && enumeration.length) {
        allData.push({
          ...getKeyName(VARIABLE_TYPE._ENUM),
          content: enumeration,
        });
      }
    }
    return allData;
  }

  return getAllData();
}
